package com.htandroid.htandroid.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.htandroid.htandroid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MyAdapter_listtabm extends BaseAdapter {
    ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    // 用来控制CheckBox的选中状况
    private HashMap<Integer, Boolean> isSelected;
    //用来传递单价修改状态
    private HashMap<Integer, String> IpriceUpdated;
    // 上下文
    Context context;
    // 用来导入布局
    FrameLayout lvparent;

    // 构造器
    public MyAdapter_listtabm(Context context, ArrayList<Map<String, Object>> data, FrameLayout lvparent) {
        this.data = data;
        this.context = context;
        this.lvparent = lvparent;
        isSelected = new HashMap<Integer, Boolean>();
        IpriceUpdated = new HashMap<Integer, String>();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return (Integer.valueOf(data.get(position).get("index").toString()) - 1);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            // 导入布局并赋值给convertview
            convertView = LayoutInflater.from(this.context).inflate(R.layout.basenvo_item_listtabm, parent, false);

            holder.background = (ImageView) convertView.findViewById(R.id.item_iv);
            holder.index = (TextView) convertView.findViewById(R.id.item_index);
            holder.cdepname_tv = (TextView) convertView.findViewById(R.id.cdepname_tv);
            holder.cinvname_tv = (TextView) convertView.findViewById(R.id.cinvname_tv);
            holder.cinvstd_tv = (TextView) convertView.findViewById(R.id.cinvstd_tv);
            holder.cinvunit_tv = (TextView) convertView.findViewById(R.id.cinvunit_tv);
            holder.iquantity_tv = (TextView) convertView.findViewById(R.id.iquantity_tv);
            holder.cmemo_tv = (TextView) convertView.findViewById(R.id.cmemo_tv);
            holder.iprice_tv = (EditText) convertView.findViewById(R.id.iprice_tv);

            holder.edit_iv = (ImageView) convertView.findViewById(R.id.edit_iv);
            holder.editover_iv = (ImageView) convertView.findViewById(R.id.editover_iv);
            //监听编辑按钮
            final ViewHolder finalHolder = holder;
            finalHolder.iprice_tv.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        InputMethodManager in = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        in.hideSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                        //软键盘回车后自动关闭编辑状态
                        finalHolder.editover_iv.performClick();
                        return true;
                    }
                    return false;
                }
            });
            holder.edit_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击进入编辑状态
                    finalHolder.edit_iv.setVisibility(View.GONE);
                    finalHolder.editover_iv.setVisibility(View.VISIBLE);
                    finalHolder.iprice_tv.setBackgroundResource(R.drawable.edit_background);
                    finalHolder.iprice_tv.setTextColor(Color.WHITE);

                    //允许编辑
                    finalHolder.iprice_tv.setEnabled(true);
                    finalHolder.iprice_tv.setFocusable(true);
                    finalHolder.iprice_tv.setFocusableInTouchMode(true);
                    finalHolder.iprice_tv.setClickable(true);

                    finalHolder.iprice_tv.clearFocus();
                    finalHolder.iprice_tv.requestFocus();
                }
            });
            //监听编辑完成按钮
            holder.editover_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击进入完成编辑
                    finalHolder.edit_iv.setVisibility(View.VISIBLE);
                    finalHolder.editover_iv.setVisibility(View.GONE);
                    finalHolder.iprice_tv.setTextColor(finalHolder.cinvstd_tv.getCurrentTextColor());
                    finalHolder.iprice_tv.setBackgroundColor(Color.TRANSPARENT);

                    //允许编辑
                    finalHolder.iprice_tv.setEnabled(false);
                    finalHolder.iprice_tv.setFocusable(false);
                    finalHolder.iprice_tv.setFocusableInTouchMode(false);
                    finalHolder.iprice_tv.setClickable(false);
                    //保存数据
                    data.get(position).put("iprice", finalHolder.iprice_tv.getText());
                    IpriceUpdated.put(position, finalHolder.iprice_tv.getText().toString());
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (data != null) {
            //convertView.setTag(R.id.tag_first, data.get(position).get("index"));
            if (holder.index != null) {
                holder.index.setText(data.get(position).get("index").toString());
            }
            if (holder.cdepname_tv != null) {
                holder.cdepname_tv.setText(data.get(position).get("cdepname").toString());
            }
            if (holder.cinvname_tv != null) {
                holder.cinvname_tv.setText(data.get(position).get("cinvname").toString());
            }
            if (holder.cinvstd_tv != null) {
                holder.cinvstd_tv.setText(data.get(position).get("cinvstd").toString());
            }
            if (holder.cinvunit_tv != null) {
                holder.cinvunit_tv.setText(data.get(position).get("cinvunit").toString());
            }
            if (holder.iquantity_tv != null) {
                holder.iquantity_tv.setText(data.get(position).get("iquantity").toString());
            }
            if (holder.cmemo_tv != null) {
                holder.cmemo_tv.setText(data.get(position).get("cmemo").toString());
            }
            if (holder.iprice_tv != null) {
                holder.iprice_tv.setText(data.get(position).get("iprice").toString());
                IpriceUpdated.put(position, data.get(position).get("iprice").toString());
            }
            int index = Integer.valueOf(data.get(position).get("index").toString()) - 1;
            if (isSelected.size() > 0 && isSelected.size() > index) {
                if (getIsSelected().get(index)) {
                    //选中
                    holder.background.setImageResource(R.drawable.itembox_choose);
                } else {
                    //未选
                    holder.background.setImageResource(R.drawable.itembox_zi);
                }
            }
        }
        return convertView;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public HashMap<Integer, String> getIpriceUpdated() {
        return IpriceUpdated;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    private class ViewHolder {
        ImageView background;
        TextView index;
        TextView cdepname_tv;
        TextView cinvname_tv;
        TextView cinvstd_tv;
        TextView cinvunit_tv;
        TextView iquantity_tv;
        TextView cmemo_tv;
        EditText iprice_tv;
        ImageView edit_iv, editover_iv;
    }
}
